package com.xayq.orap.model;

import java.util.Date;

import org.apache.commons.lang.time.DateUtils;

public class AccessToken{

	protected Date createTime = new Date();
	
    public static String BEARER_TYPE = "Bearer";

    //Default value
    public static int REFRESH_TOKEN_VALIDITY_SECONDS = 60 * 60 * 24 * 30; // default 30 days.
    //Default value
    public static int ACCESS_TOKEN_VALIDITY_SECONDS = 60 * 60 * 12; // default 12 hours.


    protected static long THOUSAND = 1000l;


    private String token;

    private String username;

    private String clientId;

    private String refreshToken;

    private String tokenType = BEARER_TYPE;

    private int tokenExpiredSeconds = ACCESS_TOKEN_VALIDITY_SECONDS;

    private int refreshTokenExpiredSeconds = REFRESH_TOKEN_VALIDITY_SECONDS;
    
    private String authenticationId;
    
    private String scope;


    public AccessToken() {
    }


    public boolean tokenExpired() {
        final long time = createTime.getTime() + (this.tokenExpiredSeconds * THOUSAND);
        return time < new Date().getTime();
    }


    public boolean refreshTokenExpired() {
        final long time = createTime.getTime() + (this.refreshTokenExpiredSeconds * THOUSAND);
        return time < new Date().getTime();
    }


    public long currentTokenExpiredSeconds() {
        if (tokenExpired()) {
            return -1;
        }
        final long time = createTime.getTime() + (this.tokenExpiredSeconds * THOUSAND);
        return (time - new Date().getTime()) / THOUSAND;
    }
    
    
    public Date getCreateTime() {
		return createTime;
	}


	public AccessToken setCreateTime(Date createTime) {
		this.createTime = createTime;
		return this;
	}


	public String getToken() {
		return token;
	}


	public AccessToken setToken(String token) {
		this.token = token;
		return this;
	}


	public String getUsername() {
		return username;
	}


	public AccessToken setUsername(String username) {
		this.username = username;
		return this;
	}


	public String getClientId() {
		return clientId;
	}


	public AccessToken setClientId(String clientId) {
		this.clientId = clientId;
		return this;
	}


	public String getRefreshToken() {
		return refreshToken;
	}


	public AccessToken setRefreshToken(String refreshToken) {
		this.refreshToken = refreshToken;
		return this;
	}


	public String getTokenType() {
		return tokenType;
	}


	public AccessToken setTokenType(String tokenType) {
		this.tokenType = tokenType;
		return this;
	}


	public int getTokenExpiredSeconds() {
		return tokenExpiredSeconds;
	}


	public AccessToken setTokenExpiredSeconds(int tokenExpiredSeconds) {
		this.tokenExpiredSeconds = tokenExpiredSeconds;
		return this;
	}


	public int getRefreshTokenExpiredSeconds() {
		return refreshTokenExpiredSeconds;
	}


	public AccessToken setRefreshTokenExpiredSeconds(int refreshTokenExpiredSeconds) {
		this.refreshTokenExpiredSeconds = refreshTokenExpiredSeconds;
		return this;
	}
	
	

	public String getAuthenticationId() {
		return authenticationId;
	}


	public AccessToken setAuthenticationId(String authenticationId) {
		this.authenticationId = authenticationId;
		return this;
	}

	
	
	public String getScope() {
		return scope;
	}


	public AccessToken setScope(String scope) {
		this.scope = scope;
		return this;
	}


	@Override
    public String toString() {
        return "{" +
                "token='" + token + '\'' +
                ", username='" + username + '\'' +
                ", clientId='" + clientId + '\'' +
                ", refreshToken='" + refreshToken + '\'' +
                ", tokenType='" + tokenType + '\'' +
                ", tokenExpiredSeconds=" + tokenExpiredSeconds +
                ", refreshTokenExpiredSeconds=" + refreshTokenExpiredSeconds +
                '}';
    }
	
	/**
     * Clone
     * Exclude token, refresh_token, authenticationId, expired
     *
     * @return New AccessToken instance
     */
    public AccessToken cloneMe() {
        
    	return new AccessToken()
			    	.setUsername(username)
			    	.setClientId(clientId)
			    	.setTokenType(tokenType);
              
    }
}
